package com.demo2do.fms.scheduler.util;

import java.io.Writer;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.demo2do.core.entity.exception.BusinessException;
import com.opencsv.CSVWriter;

/**
 *
 * @author Ark
 *
 */
public class CsvUtils {

    private static final Logger logger = LoggerFactory.getLogger(CsvUtils.class);
    
    /**
     * write list of string_array to csv_file, using default setup
     * 
     * @param lines
     * @param writer
     */
    public static void write(List<String[]> lines, Writer writer) {
        write(lines, writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, CSVWriter.DEFAULT_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
    }
    
    /**
     * write list of string_array to csv_file, using custom setup
     * 
     * @param lines
     * @param writer
     * @param separator
     * @param quotechar
     * @param escapechar
     * @param lineEnd
     */
    public static void write(List<String[]> lines, Writer writer, char separator, char quotechar, char escapechar, String lineEnd) {
        
        try {

            CSVWriter csvWriter = new CSVWriter(writer, separator, quotechar, escapechar, lineEnd);
            csvWriter.writeAll(lines);
            
            csvWriter.close();
            
        } catch (Exception e) {
            logger.error("#write() - write csv_file error={}", e);
            throw BusinessException.newInstance("500", "导出csv文件失败");
        }
        
    }
    
}
